package com.project.bohang.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bohang.common.exception.BusinessException;
import com.project.bohang.common.model.Response;
import com.project.bohang.entity.UserYmRecord;
import com.project.bohang.pojo.dto.UserYmRecordDto;
import com.project.bohang.service.UserYmRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 *  疫苗信息管理 （用户）
 *
 * @author bohang
 * @since 2022-03-26
 */
@Controller
@RequestMapping("/userYmRecord")
@ResponseBody
public class UserYmRecordController extends BaseController {

    @Autowired
    private UserYmRecordService userYmRecordService;

    /**
     * 疫苗注射信息列表(不用分页)
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public Response<List<UserYmRecord>> selectList(@RequestBody UserYmRecordDto dto){
        QueryWrapper<UserYmRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",dto.getUserId());
        queryWrapper.eq("deleted",0);
        return Response.success(userYmRecordService.list(queryWrapper));
    }

    /**
     * 登记疫苗信息
     *
     * @param req
     * @return
     */
    @PostMapping("/save")
    public Response save(@RequestBody UserYmRecord req) {
        QueryWrapper<UserYmRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",req.getUserId());
        queryWrapper.eq("yimiao_grade",req.getYimiaoGrade());
        queryWrapper.eq("deleted",0);
        UserYmRecord userYmRecord   =  userYmRecordService.getOne(queryWrapper);
        if(userYmRecord!=null){
            throw new BusinessException("700001","已经注册过该疫苗了");
        }
        userYmRecordService.save(req);
        return Response.success();
    }



    /**
     * 删除疫苗信息
     *
     * @param req
     * @return
     */
    @PostMapping("/del")
    public Response del(@RequestBody UserYmRecord req) {
        UserYmRecord entity = new UserYmRecord();
        entity.setId(req.getId());
        entity.setDeleted("1");
        userYmRecordService.updateById(entity);
        return Response.success();
    }

    /**
     * 疫苗信息详情
     *
     * @param req
     * @return
     */
    @PostMapping("/detail")
    public Response<UserYmRecord> detail(@RequestBody UserYmRecord req) {
        UserYmRecord entity = userYmRecordService.getById(req.getId());
        return Response.success(entity);
    }


}

